<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            物料条码打印
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">

          <li class="aui-list-item">
              <div class="aui-list-item-inner">
                  <div class="aui-list-item-label">物料</div>
                  <div class="aui-list-item-input">
                      <input type="text" placeholder="选择/扫描 物料" id="mrlCode" value="" onclick="getIdSelector();" />
                  </div>
                  <i class="aui-input-addon aui-iconfont aui-icon-right" onclick="goSelectMrl()"></i>
              </div>
          </li>
          <li class="aui-list-item">
              <div class="aui-list-item-inner">
                  <div class="aui-list-item-label">批次号</div>
                  <div class="aui-list-item-input">
                      <input type="text" id="lotCode"  value=""  >
                  </div>
              </div>
          </li>

          <li class="aui-list-item">
              <div class="aui-list-item-inner">
                  <div class="aui-list-item-label">数量</div>
                  <div class="aui-list-item-input">
                      <input type="number" id="qty" onchange="checkQty()">
                  </div>
              </div>
          </li>
          <li class="aui-list-item">
            <div class="aui-list-item-inner">
                <div class="aui-list-item-label">条码数量</div>
                <div class="aui-list-item-input">
                    <input type="number" id="prqty" value=1>
                </div>
            </div>
        </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">蓝牙设备</div>
                    <select id="bluetooth">
                    </select>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="bluetoothConnect()">蓝牙连接</div>
       <div style="width:100%;margin-top:2%;"></div>
       <div class="aui-btn aui-btn-info aui-btn-block" onclick="printBad()">打印</div>

    </div>

    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var mrlId = "";
    var mrlCode = "";
    var mrlName = "";

    //初始化
    apiready = function() {
        $("#workCenterCode").focus();
        idSelector = "workCenterCode";

        //获取蓝牙列表
        getBluetoothCombox();
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //查询蓝牙设备
    function getBluetoothCombox() {
        var modulePrinterCpcl = api.require('modulePrinterCpcl');
        modulePrinterCpcl.searchBluetooth({}, function(ret, err) {
            if (ret) {
                if (ret.code == "S") {
                    var list = ret.list;
                    var html = "<option value=''>--请选择--</option>";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value='" + list[i].code + "'>" + list[i].name + "</option>";
                    }
                    $api.html($api.byId("bluetooth"), html);

                    //取缓存中蓝牙设备打印机
                    var bluetooth = localStorage.getItem('bluetooth');
                    if (bluetooth != null && bluetooth != undefined && bluetooth != "") {
                        $("#bluetooth").val(bluetooth);
                    }

                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //蓝牙连接
    function bluetoothConnect() {
        var address = $("#bluetooth").val();
        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: "请选择蓝牙"
            });
            return;
        }
        var modulePrinterCpcl = api.require('modulePrinterCpcl');
        modulePrinterCpcl.connectBluetooth({
            address: address,
        }, function(ret, err) {
            if (ret) {
                // api.hideProgress();
                if (ret.code == "S") {
                    api.toast({
                        msg: ret.message
                    });
                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                // api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }


    //报废数量值改变事件
    function checkQty(){
       var qty=$("#qty").val();
       //转移数量非空校验
       if (qty == "" || qty == null || qty == undefined) {
       } else {
           if (parseFloat(qty) <= 0) {
               api.toast({
                   msg: '数量必须大于0',
                   duration: 3000
               });
               return;
           }
       }
    }


    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function(ret, err) {
            //alert(ret);扫码的结果都是编码
            if (idSelector == 'mrlCode') {

                $("#mrlCode").val(ret);
                getMrl(ret, idSelector);
            }
        });
    }





    //6：选择物料
    function goSelectMrl() {
        api.openWin({
            name: 'uwmSelectMrl',
            url: 'uwmSelectMrl.html',
            pageParam: {
                name: "paCrushingPrint_mrl"
            }
        });
    }

    //物料选择后回调函数
    function setMrl(id, code, name) {
        mrlId = id;
        mrlCode = code;
        mrlName = name;
        document.getElementById('mrlCode').value = code + ":" + name;
    }

    //7；PDA自带扫描回车事件-库位
    $("#mrlCode").on('keypress', function(e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            //物料编码
            $("#mrlCode").val(sm);
            //校验改箱码是否存在
            getMrl(sm, "mrlCode");
        }
    });

    //5:库位扫描校验
    function getMrl(code, inputId) {
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getMdMrlList";
        var data = {
            values: {
                codeQ: code,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    mrlId = list[0].sysid;
                    mrlCode = list[0].code;
                    mrlName = list[0].name;
                    document.getElementById(inputId).value = list[0].code + ":" + list[0].name;

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#mrlCode").focus();
                    $("#mrlCode").select();
                }
            }
        });
    }




    //录数打印
    function printBad() {

        var address = $("#bluetooth").val();

        //取批次号
        var lotCode = $("#lotCode").val();

        //数量
        var qty = $("#qty").val();

        //条码数量
        var prqty = $("#prqty").val();

        //打印机非空校验
        if (address == null || address == "" || address == undefined) {
            api.toast({
                msg: '请选择打印机!',
                duration: 3000
            });
            return;
        }

        //物料号非空校验
        if (mrlCode == null || mrlCode == "" || mrlCode == undefined) {
            api.toast({
                msg: '请选择或扫描物料',
                duration: 3000
            });
            return;
        }

        //批次号非空校验
        if (lotCode == null || lotCode == "" || lotCode == undefined) {
            api.toast({
                msg: '请输入批次号',
                duration: 3000
            });
            return;
        }

        //转移数量非空校验
        if (qty == "" || qty == null || qty == undefined) {
            api.toast({
                msg: '请输入数量',
                duration: 3000
            });
            return;
        } else  {
            if (parseFloat(qty) <= 0) {
                api.toast({
                    msg: '数量必须大于0',
                    duration: 3000
                });
                return;
            }
        }

        if (prqty == "" || prqty == null || prqty == undefined) {
            api.toast({
                msg: '请输入条码数量',
                duration: 3000
            });
            return;
        } else  {
            if (parseFloat(prqty) <= 0) {
                api.toast({
                    msg: '条码数量必须大于0',
                    duration: 3000
                });
                return;
            }
        }



        localStorage.setItem('bluetooth', address);

        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/paCrushingTmCreateSave";
        var data = {
            values: {
                mrlId: mrlId,
                mrlCode :mrlCode,
                mrlName :mrlName,
                lotCode :lotCode,
                qty: qty,
                prqty: prqty,
                un: un
            }
        };
        // for(var x = 0 ; x < prqty; x++){
            api.ajax({
                url: url,
                method: 'post',
                dataType: 'json',
                timeout: 300,
                returnAll: false,
                data: data
            }, function(ret, err) {
                if (ret) {
                    var data = JSON.stringify(ret);
                    var myobj = eval("(" + data + ")");

                    //返回成功
                    if (myobj.code == "0") {

                        var json = myobj.json;
                        //调用PDA打印
                        printPDA(json, address);

                    } else if (myobj.code == "1") {

                        api.toast({
                            msg: myobj.message,
                            duration: 3000
                        });
                    }
                }
            });
            // sleep(1000);
        // }

    }

    function sleep(numberMillis) {
        var now = new Date();
        var exitTime = now.getTime() + numberMillis;
        while (true) {
            now = new Date();
            if (now.getTime() > exitTime)
                return true;
        }
    }





    //打印
    function printPDA(json, address) {
        
        // var modulePrinterCT = api.require('modulePrinterCT'); 注释掉原3*5打印改为4*6打印
        // modulePrinterCT.printerPlatform({
        var modulePrinterCpcl = api.require('modulePrinterCpcl');
        //modulePrinterCpcl.printTemplateSixLineFourBySixQR({    2023-05-25 gaomq 4*6六行模板改为4*6五行模板
        modulePrinterCpcl.printTemplateFiveLineFourBySixQR({
            json: json, //打印数据JSON
            address: address //蓝牙地址
        }, function(ret, err) {
            if (ret) {
                if (ret.code == "S") {
                    api.toast({
                        msg: "打印成功"
                    });
                } else {
                    api.toast({
                        msg: ret.message
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }



    //订单返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
